window.uxe = window.uxe || { };

uxe.Class = function(properties) {
  var constructor = properties.constructor || function() { };
  var classProperties = properties.classProperties || { };
  
  $.extend(constructor.prototype, properties);
  $.extend(constructor, classProperties);
  
  delete constructor.prototype.constructor;
  delete constructor.prototype.classProperties;
  
  return constructor;
};


uxe.Product = uxe.Class({
  classProperties: {
    ALL: [
	    [ 'market', 'Android Market' ],
	    [ 'authenticator', 'Authenticator' ],
	    [ 'blog', 'Blog Search' ],
	    [ 'blogger', 'Blogger' ],
	    [ 'books', 'Books' ],
	    [ 'calendar', 'Calendar' ],
	    [ 'chrome', 'Chrome' ],
	    [ 'webstore', 'Chrome Webstore' ],
	    [ 'contacts', 'Contacts' ],
	    [ 'dictionary', 'Dictionary' ],
	    [ 'directions', 'Directions' ],
	    [ 'docs', 'Docs' ],
	    [ 'ebooks', 'eBooks' ],
	    [ 'earth', 'Earth' ],
	    [ 'fastflip', 'FastFlip' ],
	    [ 'finance', 'Finance' ],
	    [ 'goggles', 'Goggles' ],
	    [ 'favicon', '' ],
	    [ 'evenmore', 'Even More' ],
	    [ 'mail', 'Gmail' ],
	    [ 'mailoffline', 'Gmail' ],
	    [ 'offers', 'Offers' ],
	    [ 'plus', '+Chris' ],
	    [ 'wallet', 'Wallet' ],
	    [ 'groups', 'Groups' ],
	    [ 'help', 'Help' ],
	    [ 'igoogle', 'iGoogle' ],
	    [ 'images', 'Images' ],
	    [ 'latitude', 'Latitude' ],
	    [ 'maps', 'Maps' ],
	    [ 'mobile', 'Mobile' ],
	    [ 'music', 'Music' ],
	    [ 'news', 'News' ],
	    [ 'notebook', 'Notebook' ],
	    [ 'orkut', 'Orkut' ],
	    [ 'photos', 'Photos' ],
	    [ 'picasa', 'Picasa' ],
	    [ 'picnik', 'Picnik' ],
	    [ 'qa', 'Q&A' ],
	    [ 'places', 'Places' ],
	    [ 'reader', 'Reader' ],
	    [ 'scholar', 'Scholar' ],
	    [ 'scratchpad', 'Scratchpad' ],
	    [ 'serp', 'Search' ],
	    [ 'searchpointer', 'Search' ],
	    [ 'shopping', 'Shop' ],
	    [ 'sites', 'Sites' ],
	    [ 'skymap', 'Skymap' ],
	    [ 'sms', 'SMS Search' ],
	    [ 'sync', 'Sync' ],
	    [ 'talk', 'Talk' ],
	    [ 'tasks', 'Tasks' ],
	    [ 'transit', 'Transit' ],
	    [ 'translate', 'Translate' ],
	    [ 'video', 'Video' ],
	    [ 'voice', 'Voice' ],
	    [ 'weather', 'Weather' ],
	    [ 'youtube', 'YouTube' ],
	    [ 'drive', 'Drive' ],
	    [ 'play', 'Play' ],
			[ 'newproduct', 'Promo 1' ],
			[ 'larry', 'Call Larry' ],
			[ 'hangout', 'Try Hangout' ],
			[ 'profile', 'Chris Wiggins' ],
			[ 'messages', 'Messages' ],
			[ 'shop', 'Shop' ],
			[ 'home', 'home' ]
    ],
    
    DEFAULT: 'home',
    SEARCH: null,
    
		TOP_MARS: [
			'profile',
			'serp',
			'messages',
			'streams',
			'shop',
			'play',
			'plan',
			'mystuff'
		],

		RECENT: [
			'calendar',
      'contacts',
      'docs',
      'drive',
			'images',
			'news'
    ],
		
    TOP: [
	    'plus',
	    'serp',
	    'shop',
	    'maps',
	    'mail',
      'play',
      'youtube',
			'news',
      'larry'
    ],
    
    TOP_LEGACY: [
      'plus',
      'serp',
      'images',
      'maps',
      'youtube',
      'news',
      'mail',
      'docs',
      'calendar'
    ],
    
    MORE: [
      'books',
      'blogger',
      'earth',
      'finance',
      'goggles',
      'groups',
      'latitude',
      'mobile',
      'music',
      'offers',
      'photos',
      'reader',
      'scholar',
      'sites',
      'shop',
      'skymap',
      'talk',
      'tasks',
      'transit',
      'translate',
      'video',
      'voice',
      'wallet'
    ],
    
    MORE_LEGACY: [
      'sites',
      'groups',
      'contacts',
      'translate',
      'mobile',
      'books',
      'music',
      'offers',
      'wallet',
      'shop',
      'blogger',
      'reader',
      'finance',
      'photos',
      'video'
    ],
    
    MUCH_MORE: [
      'market',
      'authenticator',
      'blog',
      'blogger',
      'books',
      'chrome',
      'webstore',
      'contacts',
      'dictionary',
      'directions',
      'ebooks',
      'earth',
      'finance',
      'goggles',
      'groups',
      'igoogle',
      'latitude',
      'mobile',
      'music',
      'notebook',
      'offers',
      'orkut',
      'photos',
      'picasa',
      'picnik',
      'places',
      'reader',
      'scholar',
      'scratchpad',
      'shop',
      'sites',
      'skymap',
      'sms',
      'sync',
      'talk',
      'tasks',
      'transit',
      'translate',
      'video',
      'voice',
      'wallet',
      'weather'
    ],
    
    ID_TO_PRODUCT_: { },
    
    forID: function(id) {
      return uxe.Product.ID_TO_PRODUCT_[id];
    }
  },
  
  constructor: function(id, name, offsetX, offsetY) {
    this.id = id;
    this.name = name;
    this.aliasFor = null;
    this.offsetX_ = offsetX;
    this.offsetY_ = offsetY;
    uxe.Product.ID_TO_PRODUCT_[id] = this;
  },
  
  getOffsetX: function(iconWidth) {
    return iconWidth * this.offsetX_;
  },
  
  getOffsetY: function(iconHeight) {
    return iconHeight * this.offsetY_;
  },
  
  clone: function(newID, opt_newName) {
    var newName = opt_newName || this.name;
    return new uxe.Product(newID, newName, this.offsetX_, this.offsetY_);
  },
  
  alias: function(newID) {
    var newProd = new uxe.Product(newID, this.name, this.offsetX_, this.offsetY_);
    newProd.aliasFor = this;
    return newProd;
  },
  
  dealias: function() {
    return this.aliasFor || this;
  }
});


(function() {
  var ICON_ROWS = 10;
  
  var products = uxe.Product.ALL;
  
  for (var i = 0, n = products.length; i < n; i++) {
    var product = products[i];
    var col = Math.floor(i / ICON_ROWS);
    var row = i % ICON_ROWS;
    
    new uxe.Product(product[0], product[1], -col, -row);
  }
  
  var forID = uxe.Product.forID;
  uxe.Product.ALL = uxe.Product.ALL.map(forID);

  uxe.Product.TOP = uxe.Product.TOP.map(forID);
  uxe.Product.TOP_MARS = uxe.Product.TOP_MARS.map(forID);

  uxe.Product.TOP_LEGACY = uxe.Product.TOP_LEGACY.map(forID);
  uxe.Product.MORE = uxe.Product.MORE.map(forID);
  uxe.Product.MORE_LEGACY = uxe.Product.MORE_LEGACY.map(forID);
  uxe.Product.RECENT = uxe.Product.RECENT.map(forID);
  uxe.Product.MUCH_MORE = uxe.Product.MUCH_MORE.map(forID);
  uxe.Product.DEFAULT = forID(uxe.Product.DEFAULT);

  uxe.Product.SEARCH = forID('serp').alias('search');
})();


uxe.TaskMonitor = uxe.Class({
  sessionId_: null,
  startTaskLink_: null,
  startTaskPrimed_: false,
  
  constructor: function(sessionId) {
    this.sessionId_ = sessionId;
  },
  
  start: function() {
    this.startTaskLink_ = $('<a id="logStartTask" class="logStartTask" accesskey="s" href="#">GO</a>');
    this.startTaskLink_.appendTo('body');
    this.startTaskLink_.click( $.proxy(this.onStartTask_, this) );
    
    // gprotolog.init(this.sessionId_, true);
    // gprotolog.pause();
  },
  
  onStartTask_: function() {
    if (!this.startTaskPrimed_) {
      this.startTaskLink_.addClass('logTaskFlash');
      this.startTaskPrimed_ = true;
    } else {
      // gprotolog.resume();
      // gprotolog.log('Task started');
      this.startTaskLink_.removeClass('logTaskFlash');
      this.startTaskPrimed_ = false;
    }
    return false;
  },
  
  hideCue_: function() {
    this.startTaskLink_.removeClass('logTaskFlash');
  }
});


uxe.VascoApp = uxe.Class({
  constructor: function() {
    this.mru_ = [ ];
    uxe.VascoApp.instance = this;
  },

  mru_: null,
  onProductChange: null,
  lastActiveProduct_: null,
  
  getActiveProduct: function() {
    return this.lastActiveProduct_ || this.fetchActiveProduct_();
  },
  
  fetchActiveProduct_: function() {
    var app = window.location.hash.substr(1);
    return app ?
      (uxe.Product.forID(app) || uxe.Product.DEFAULT) :
      uxe.Product.SEARCH;
  },
  
  getMRUProducts: function() {
    return this.mru_;
  },
  
  setActiveProduct: function(product) {
    if (typeof product == 'string') {
      product = uxe.Product.forID(product) || uxe.Product.DEFAULT;
    }
    window.location.hash = '#'+product.id;
  },
  
  run: function() {
    window.onpopstate = $.proxy(this.onPopState_, this);
    this.onPopState_();
  },
  
  onPopState_: function() {
    var product = this.fetchActiveProduct_();
    if (product == this.lastActiveProduct_) {
      return;
    }
    
    this.lastActiveProduct_ = product;
    $('html').attr('data-product', product.id);

   	$('#app-bg-label').text(product.name.replace('&nbsp;',' '));
    
    this.updateMRU_( product.dealias() );
    
    if (this.onProductChange) {
      this.onProductChange.call(this, product);
    }
  },
  
  updateMRU_: function(product) {
    if (uxe.Product.TOP.indexOf(product) >= 0) {
      return;
    }
    
    var i = this.mru_.indexOf(product);
    if (i >= 0) {
      this.mru_.splice(i, 1);
    } else if (this.mru_.length == 4) {
      this.mru_.splice(3, 1);
    }
    this.mru_.splice(0, 0, product);
  }
});
